package industry;

import java.util.Random;

import main.Plugin;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.inventory.ItemStack;

import actives.ActiveManager;
import actives.ActiveType;
import actives.Active.ActiveState;

import skills.Skill;
import skills.Skills;
import skills.SkillHandler;

public class Mining implements Listener {

	public Mining(Plugin plugin) {
		Bukkit.getPluginManager().registerEvents(this, plugin);
	}

	@EventHandler
	private void blockDamage(BlockDamageEvent event) {
		Player player = event.getPlayer();
		boolean pickaxe = player.getItemInHand().getType().name().contains("_PICKAXE");
		if (pickaxe) {
			Block block = event.getBlock();
			if (isMinable(block)) {
				if (ActiveManager.getState(player, ActiveType.MINING).equals(ActiveState.ACTIVE)) {
					block.breakNaturally();
				}
			}
		}
	}

	@EventHandler
	private void blockBreak(BlockBreakEvent event) {
		Player player = event.getPlayer();
		boolean pickaxe = player.getItemInHand().getType().name().contains("_PICKAXE");
		if (pickaxe) {
			Block block = event.getBlock();
			if (isMinable(block)) {
				Skill mining = SkillHandler.getSkill(player, Skills.MINING);
				mining.addExp(getExp(block));
				ItemStack drop = getDrop(player, block);
				if (drop != null) {
					World world = block.getWorld();
					Location location = block.getLocation();
					world.dropItemNaturally(location, drop);
				}				
			}
		}
	}

	public static boolean isMinable(Block block) {
		switch (block.getType()) {
		case STONE:
		case OBSIDIAN:
		case NETHERRACK:
		case DIAMOND_ORE:
		case ENDER_STONE:
		case IRON_ORE:
		case REDSTONE_ORE:
		case EMERALD_ORE:
		case COAL_ORE:
		case LAPIS_ORE:
		case GOLD_ORE:
			return true;
		default:
			return false;
		}
	}

	private int getExp(Block block) {
		switch (block.getType()) {
		case NETHERRACK:
			return 8;
		case ENDER_STONE:
			if (block.getBiome() == Biome.SKY)
				return 13;
			else
				return 0;
		case STONE:
		case DIAMOND_ORE:
		case IRON_ORE:
		case REDSTONE_ORE:
		case EMERALD_ORE:
		case COAL_ORE:
		case LAPIS_ORE:
		case GOLD_ORE:
			return 10;
		case OBSIDIAN:
			return 30;
		default:
			return 0;
		}
	}

	private ItemStack getDrop(Player player, Block block) {
		if (SkillHandler.getSkill(player, Skills.MINING).level() >= 500) {
			double chance = .3 * SkillHandler.getSkill(player, Skills.MINING).level() / 1000;
			boolean proc = new Random().nextDouble() <= chance;
			if (proc) {
				switch(block.getType()) {
				case DIAMOND_ORE:
				case EMERALD_ORE:
				case REDSTONE_ORE:
				case GOLD_ORE:
				case LAPIS_ORE:
					return block.getDrops().iterator().next();
				default:
					break;		
				}
			}
		}
		return null;
	}
}
